package com.come4loves.game.module.fight.action;

import java.util.ArrayList;
import java.util.List;

import com.come4loves.game.module.battle.helper.PBConvertHelper;
import com.come4loves.game.proto.BattleProto;
import com.google.protobuf.GeneratedMessage.Builder;

public class BatchAction extends Action {
	private List<Action> actions;
	public BatchAction() {
		super(0);
		this.actions = new ArrayList<Action>();
	}
	public BatchAction(List<Action> actions) {
		super(0);
		this.actions = actions;
	}
	
	public void addAction(Action action) {
		this.actions.add(action);
	}
	
	public List<Action> getActions() {
		return actions;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (Action action : actions) {
			sb.append(action.toString()).append("\n");
		}
		return sb.toString();
	}
	/**
	 * 清理空白或者少于1个的batch
	 * @return
	 */
	public Action trim(){
		if (this.actions.isEmpty()) {
			return null;
		}
		if (this.actions.size() == 1) {
			return this.actions.get(0);
		}
		return this;
	}

	@Override
	public Builder<?> toProtocolBufferBuilder() {
		BattleProto.BatchAction.Builder builder = BattleProto.BatchAction.newBuilder();
		builder.addAllActs(PBConvertHelper.toGeneralAct(this.actions));
		return builder;
	}

}
